package com.yk.i_wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yk.i_wms.mapper.PdtBomMapper;
import com.yk.i_wms.model.PdtBom;
import com.yk.i_wms.service.IPdtBomService;
import com.yk.i_wms.vo.BomTreeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * BOM

 *
 * @author hbq
 * @date 2021-03-29 16:32:54
 */
@Slf4j
@Service
public class PdtBomServiceImpl extends ServiceImpl<PdtBomMapper, PdtBom> implements IPdtBomService {
    @Resource
    private PdtBomMapper pdtBomMapper;
    /**
     * 列表
     * @param params
     * @return
     */
    public Page<Map> findList(Map<String, Object> params){
        Integer pageNum = MapUtils.getInteger(params, "pageNum");
        Integer pageSize = MapUtils.getInteger(params, "pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = -1;
        }
        Page<Map> pages = new Page<>(pageNum, pageSize);
        return pdtBomMapper.findList(pages, params);
    }

    /**
     * 通过机型获取所有底层子级列表
     * @param modelCode
     * @return
     */
    @Override
    public List<PdtBom> getMinSonList(String modelCode) {
        PdtBom pdtBom = pdtBomMapper.selectOne(new LambdaQueryWrapper<PdtBom>()
                .eq(PdtBom::getItemCode, modelCode)
                .eq(PdtBom::getParentId, 0)
        );
        List<PdtBom> minChildCodeList=new ArrayList<>();
        List<PdtBom> pdtBoms = recursionGetMinChildList(minChildCodeList, pdtBom);
        return pdtBoms;
    }
    /**
     * 递归查找当前对象的最底层对象并封装
     * @param minChildCodeList 当前获取到的最底层级别列表
     * @param pdtBom   父级对象
     * @return 全部最底层级别列表
     */
    public List<PdtBom> recursionGetMinChildList(List<PdtBom> minChildCodeList, PdtBom pdtBom) {
        List<PdtBom> pdtBoms = list(new LambdaQueryWrapper<PdtBom>().eq(PdtBom::getParentId, pdtBom.getId()));
        //寻找不到下级时，说明此记录已经是当前的最下级
        if (pdtBoms.isEmpty()){
            minChildCodeList.add(pdtBom);
        }else {
            for (PdtBom bom : pdtBoms) {
                recursionGetMinChildList(minChildCodeList,bom);
            }
        }
        return minChildCodeList;
    }
    @Override
    public List<BomTreeVO> getTreeListByModelCode(String modelCode) {
        List<PdtBom> pdtBoms = pdtBomMapper.selectList(new LambdaQueryWrapper<PdtBom>()
                .eq(PdtBom::getItemCode, modelCode)
        );
        List<BomTreeVO> bomTreeVOs=new ArrayList<>();
        for (PdtBom pdtBom : pdtBoms) {
            BomTreeVO bomTreeVO = new BomTreeVO();
            BeanUtil.copyProperties(pdtBom,bomTreeVO);
            List<BomTreeVO> treeListByParentId = getTreeListByParentId(bomTreeVO.getId());
            bomTreeVO.setChildren(treeListByParentId);
            bomTreeVOs.add(bomTreeVO);
        }
        return bomTreeVOs;
    }

    @Override
    public List<PdtBom> getRootListByModelCode(String itemCode) {
        List<PdtBom> pdtBomList = list(new LambdaQueryWrapper<PdtBom>().eq(PdtBom::getItemCode, itemCode));
        List<PdtBom> resultBomsList = new ArrayList<>();
        for (PdtBom pdtBom : pdtBomList) {
            resultBomsList.add(getRootListByParentId(pdtBom.getParentId()));
        }
        return resultBomsList;
    }
    public PdtBom getRootListByParentId(Long parentId){
        PdtBom pdtBom = getById(parentId);
        if (pdtBom == null) {
            return null;
        }
        if (pdtBom.getParentId()==0){
            return pdtBom;
        }
        return getRootListByParentId(pdtBom.getParentId());
    }

    public List<BomTreeVO> getTreeListByParentId(Long parentId) {
        List<BomTreeVO> bomTreeVOS = pdtBomMapper.getTreeListByParentId(parentId);
        if(!bomTreeVOS.isEmpty()){
            for (BomTreeVO treeVO : bomTreeVOS) {
                treeVO.setChildren(getTreeListByParentId(treeVO.getId()));
            }
        }
        return bomTreeVOS;
    }

}
